<?php
// +----------------------------------------------------------------------
// | EfarHost [ Easy Far And Easy More...]
// +----------------------------------------------------------------------
// | Copyright (c) 2008-2017 http://efarhost.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 琴瑟琵琶 <405146868@qq.com> <efarhost@foxmail.com>
// +----------------------------------------------------------------------
// 
namespace app\company\controller;
use app\company\model\Company as CompanyModel;
use app\company\model\Outfall as OutfallModel;

class Company extends Base
{	

	public function index(){
		$company  = new CompanyModel;
		$list = $company->order('id DESC')->paginate();
		$this->assign('list', $list);
		return $this->fetch();
	}

	public function add(){
		if (request()->isPost()) {

			$company  = new CompanyModel;
			$result = $company->allowField(true)
		    ->data(request()->post(), true)
		    ->save();

		    if($result){
		    	$this->success('添加成功','index');
		    }else{
		    	$this->error('添加失败');
		    }

		}else{
			return $this->fetch();
		}
		
	}

	public function show($id){
			$company  = new CompanyModel;
			$item =$company::get($id);
			$this->assign('item',$item);
			return $this->fetch();
	}

	public function edit($id){
		if (request()->isPost()) {
			
			$company  = new CompanyModel;
			$result = $company->isUpdate(true)->save(request()->post());
		    if($result){
		    	$this->success('修改成功','index');
		    }else{
		    	$this->error('修改失败');
		    }

		}else{
			$company  = new CompanyModel;
			$item =$company::get($id);
			$this->assign('item',$item);
			return $this->fetch();
		}
	}

	public function delete($id){

		$company  = new CompanyModel;
		$result = $company::destroy($id);
		if($result){
			$this->success('删除成功','index');
		}else{
			$this->error('删除失败');
		}
	}

	public function outfall($cid=''){
		$outfall  = new OutfallModel;
		$list = $outfall->order('id DESC')->paginate();
		$this->assign('list', $list);
		return $this->fetch();
	}

	public function outfall_add(){
	if (request()->isPost()) {

			$outfall  = new OutfallModel;
			$result = $outfall->allowField(true)
		    ->data(request()->post(), true)
		    ->save();

		    if($result){
		    	$this->success('添加成功','outfall');
		    }else{
		    	$this->error('添加失败');
		    }

		}else{
			$company  = new CompanyModel;
			$comList = $company->where('status',1)->order('id DESC')->field('id,name')->select();
			$this->assign('company',$comList);
			return $this->fetch();
		}
		
	}

	public function outfall_show($id){
		  $outfall  = new OutfallModel;
			$item =$outfall::get($id);
			$company  = new CompanyModel;
			$comList = $company->where('status',1)->order('id DESC')->field('id,name')->select();
			$this->assign('company',$comList);
			$this->assign('item',$item);
			return $this->fetch();
	}

	public function outfall_edit($id){
		if (request()->isPost()) {
			$outfall  = new OutfallModel;
			$result = $outfall->isUpdate(true)->save(request()->post());
		    if($result){
		    	$this->success('修改成功','outfall');
		    }else{
		    	$this->error('修改失败');
		    }

		}else{
			$outfall  = new OutfallModel;
			$item =$outfall::get($id);
			$company  = new CompanyModel;
			$comList = $company->where('status',1)->order('id DESC')->field('id,name')->select();
			$this->assign('company',$comList);
			$this->assign('item',$item);
			return $this->fetch();
		}
	}

	public function outfall_delete($id){
		$outfall  = new OutfallModel;
		$result = $outfall::destroy($id);
		if($result){
			$this->success('删除成功','outfall');
		}else{
			$this->error('删除失败');
		}
	}

	
}